/*
  gulpfile.js
*/

var gulp = require('gulp'),
    del = require('del'),
    imagemin = require('gulp-imagemin'),
    sass = require('gulp-sass'),
    autoprefixer = require('gulp-autoprefixer'),
    rename = require('gulp-rename'),
    minifycss = require('gulp-minify-css'),
    path = require('path'),
    glob = require('glob'),
    browserSync = require('browser-sync'),
    webpackConfig = require("./webpack.config.js"),
    reload = browserSync.create().reload;

var webpack = require('gulp-webpack'),
    uglify = require('gulp-uglify');

var dest = './dist',
    src = './src',
    _project = '/**';

var config = {
  clean: {
    dest: [dest]
  },
  browserSync: {
    server: {
      baseDir: [src , dest]//"./" 也可为数组[dest , src]
    },
    files: [
      dest + '/**' // , src + '/index/**'
    ]
  },
  img: {
    src: src + '/img/**/*.{png,jpg,gif,ico}' ,
    dest: dest + '/img'
  },
  css: {
    src: [src + '/lib/**.css', src + '/common/common.scss', src + _project + '/sass/**.scss'],
    dest: dest + '/css'
  },
    scripts: {
		src: src + '/**/**.js',
		dest: dest + '/js'
    },
  html: {
      src: src + _project + '/**.html',
      dest: dest + '/www'
  },
  font: {
     src: src + '/**/font/**',
     dest: dest + '/css'
  }
};

/* 开发过程 */
gulp.task('default', gulp.series(
    clean,
    gulp.parallel(font,css, img, scripts, html)
    ,gulp.parallel(server, watch)
));

/* 发布 */
gulp.task('build', gulp.series(
    clean,
    gulp.parallel(css, img, scripts, html)
));

/* task:clean */
function clean() {
  return del(config.clean.dest)
}

/* task:img 复制所有图片到指定目录 */
function img() {
  return gulp.src(config.img.src)
      .pipe(imagemin({optimizationLevel: 5}))
      .pipe(rename({dirname: ''}))
      .pipe(gulp.dest(config.img.dest));
}

/* task:css 合并、编译、压缩CSS */
function css() {
  return gulp.src(config.css.src)
      .pipe(rename({dirname: ''}))
      .pipe(sass())
      .pipe(autoprefixer({
        browsers: ['last 2 versions' , '> 1%',
          'ie >= 7' ,'Firefox >= 20' ,
          'last 3 Safari versions',
          'last 3 Opera versions'],
        cascade: true, //是否美化属性值 默认：true 像这样：
        //-webkit-transform: rotate(45deg);
        //        transform: rotate(45deg);
        remove:false //是否去掉不必要的前缀 默认：true
      }))
      .pipe(gulp.dest(config.css.dest))
      .pipe(rename({ suffix: '.min' }))
      .pipe(minifycss())
      .pipe(gulp.dest(config.css.dest))
      .pipe(reload({stream: true}));
}

/*font*/
function font() {
    return gulp.src(config.font.src)
        .pipe(rename({dirname: ''}))
        .pipe(gulp.dest(config.font.dest));
}

/* task:scripts webpack合并、编译、压缩JAVASCRIPTS */
function scripts() {
    return gulp.src(config.scripts.src)
        .pipe(gulp.dest(config.scripts.dest));
        //.pipe(uglify())
        //.pipe(rename({ suffix: '.min' }))
        //.pipe(rev())
        //.pipe(gulp.dest(config.scripts.dest))
        //.pipe(rev.manifest())
        //.pipe(gulp.dest('./build/rev/lib'));
}

/* task:转移HTML（若以后改为单页应用，由JS刷新整个页面，可使用webpack的html-webpack-plugin插件）*/
function html() {
    return gulp.src(config.html.src)
        .pipe(rename({dirname: ''}))
        .pipe(gulp.dest(config.html.dest));
}

/* task:server 启动服务、同步刷新页面 */
function server() {
    return browserSync(config.browserSync);
}


/* task:watch 监听 */
function watch() {
    gulp.watch(config.img.src, img);
    gulp.watch([src + '/**/**/**.scss', src + '/**/**.css'], css);
    gulp.watch(src + '/**/**.js', scripts);
    gulp.watch(config.html.src, html);
}